{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e512f616-3c8c-4c98-b23b-ed41fe964997",
   "metadata": {},
   "outputs": [],
   "source": [
    " \n",
    "# Import necessary libraries\n",
    "from nltk.parse import stanford\n",
    "from nltk.tree import ParentedTree\n",
    "\n",
    "# Set the path to the Stanford Parser\n",
    "stanford_parser_path = 'stanford-parser-full-2018-10-17/stanford-parser.jar'\n",
    "stanford_models_path = 'stanford-parser-full-2018-10-17/stanford-parser-3.9.2-models.jar'\n",
    "\n",
    "# Set the path to the input file\n",
    "input_file_path = 'input.txt'\n",
    "\n",
    "# Set the path to the output file\n",
    "output_file_path = 'output.txt'\n",
    "\n",
    "# Set the target entities\n",
    "entity1 = 'apple'\n",
    "entity2 = 'orange'\n",
    "\n",
    "# Load the Stanford Parser\n",
    "parser = stanford.StanfordParser(path_to_jar=stanford_parser_path, path_to_models_jar=stanford_models_path)\n",
    "\n",
    "# Parse the input file\n",
    "with open(input_file_path, 'r') as f:\n",
    "    sentences = f.readlines()\n",
    "\n",
    "sentences = [\"A trillion gallons of water have been poured into an empty region of outer space.\", ]\n",
    "# Find the shortest dependency path between the target entities\n",
    "with open(output_file_path, 'w') as f:\n",
    "    for sentence in sentences:\n",
    "        tree = ParentedTree.fromstring(str(list(parser.raw_parse(sentence))[0]))\n",
    "        entity1_node = None\n",
    "        entity2_node = None\n",
    "        for subtree in tree.subtrees():\n",
    "            if subtree.label() == 'NP' and entity1 in subtree.leaves():\n",
    "                entity1_node = subtree\n",
    "            elif subtree.label() == 'NP' and entity2 in subtree.leaves():\n",
    "                entity2_node = subtree\n",
    "            if entity1_node is not None and entity2_node is not None:\n",
    "                break\n",
    "        if entity1_node is None or entity2_node is None:\n",
    "            f.write('No path found between {} and {}\\n'.format(entity1, entity2))\n",
    "        else:\n",
    "            path = []\n",
    "            node = entity1_node\n",
    "            while node != tree:\n",
    "                path.append(node)\n",
    "                node = node.parent()\n",
    "            node = entity2_node\n",
    "            while node != tree:\n",
    "                if node in path:\n",
    "                    path = path[:path.index(node)+1]\n",
    "                    break\n",
    "                path.append(node)\n",
    "                node = node.parent()\n",
    "            f.write('Shortest dependency path between {} and {}: {}\\n'.format(entity1, entity2, ' -> '.join([str(node[0]) for node in path])))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a46ee898-1e42-43b2-8475-a3c967c5eb9b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
